/****************************************************************
 ** Product  :   HP Subscriber Network Application Policy
 ** Module   :   gr-manager
 ** Date: Apr 10, 2015               
 ** Author: Chen Zhenhua
 ** (C) Copyright 2015, Hewlett-Packard Company, All Rights Reserved.
 ** This software is the proprietary information of HP, Inc.  
 ** Use is subject to license terms.
 *****************************************************************
 ** Description:   
 **      It is a Atomic action to check SGM status 
 **    
 ****************************************************************/
package com.hp.snap.gr.action.impl;

import com.hp.snap.gr.exception.ActionException;
import com.hp.snap.gr.exception.BusinessException;
import com.hp.snap.gr.model.jaxb.ActionType;
import com.hp.snap.gr.model.jaxb.GeneralActionType;
import com.hp.snap.gr.model.jaxb.ParameterType;
import com.hp.snap.gr.utils.Constants;
import com.hp.snap.gr.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * 
 * It is a Atomic action to check SGM status.if Check status correct, the action complete successfully, if check failed,
 * the action throw a BusinessException and beak the action plan. In the action configuration, "Remote" and
 * "Enable_Status/Disable_Status" parameters can be set.
 * 
 * <p>
 * Remote: if set "True", the SGM status is remote SGM status, if set "False". the SGM status is local SGM status.
 * Enable_Status: Check if SGM status is not the "Enable_Status" value, the action throw a BusinessException and break
 * the execution. Disable_Status: Check if the SGM status is the "Disable_Status" value, the action throw a
 * BusinessException and break the execution.
 * 
 * 
 * <P>
 * Version: 3.3 <br>
 * Author: Chen Zhenhua Date: Apr 10, 2015
 * 
 * </P>
 * 
 **/
public class CheckSGMStatusOfEnableActionHandler extends AbstractActionHandler {

    private static Logger logger = LoggerFactory.getLogger(CheckSGMStatusOfEnableActionHandler.class);

    @Override
    public void execute(ActionType action,
                        Map<String, Object> actionPlanInputs,
                        Map<String, Object> actionPlanOutputs) throws ActionException {

        GeneralActionType actionType = (GeneralActionType) action;
        List<ParameterType> parameterTypes = actionType.getParameter();
        Map<String, String> parameterMap = Utils.contructParamMap(parameterTypes);

        String status = parameterMap.get("Enable_Status");
        String[] enableStatus = status.split(",");
//        String dis_status = parameterMap.get("Disable_Status");
        boolean remote = Boolean.parseBoolean(parameterMap.get("Remote"));
        String sgmStatus = remote ? (String) actionPlanOutputs.get(Constants.STATUS_REMOTE_SGM) : (String) actionPlanOutputs.get(Constants.STATUS_LOCAL_SGM);
        if (logger.isDebugEnabled()) {
            logger.debug("The enable status is {}", status);
//            logger.debug("The disable status is {}", dis_status);
            logger.debug("The SGM status is {}", sgmStatus);
        }
        boolean checkResult = false;
        if (sgmStatus == null) {
            throw new ActionException("get the remote SGM status failed, return a null!!");
        }
        for(String st:enableStatus){
            if (st != null && sgmStatus.equalsIgnoreCase(st)) {
                checkResult = true;
                break;
            }
        }
        if(!checkResult)
            throw new BusinessException("The SGM status is not in the '" + status + "'!!");
        
    }

}
